package com.umxwe.kill.core.template;

import com.umxwe.kill.core.domain.Result;
import com.umxwe.kill.core.domain.ResultCode;
import org.apache.poi.ss.formula.functions.T;

/**
 * 响应结果生成工具
 */
public class ResultGenerator {
    private static final String DEFAULT_SUCCESS_MESSAGE = "SUCCESS";

    public static Result<?> genSuccessResult() {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.SUCCESS);
        result.setMessage(DEFAULT_SUCCESS_MESSAGE);
        return result;
    }

    public static <T> Result<T> genSuccessResult(T data) {
        Result<T> t = new Result<T>();
        t.setCode(ResultCode.SUCCESS);
        t.setMessage(DEFAULT_SUCCESS_MESSAGE);
        t.setData(data);
        return t;
    }

    public static Result<?> genFailResult(String message) {
        Result<?> r = new Result<>();
        r.setCode(ResultCode.EXCEPTION_ERROR);
        r.setMessage(message);
        return r;
    }


    public static Result<?> genResult(boolean flag, String failMsg) {
        Result<?> result = new Result<>();
        if (flag) {
            result.setCode(ResultCode.SUCCESS);
            result.setMessage(DEFAULT_SUCCESS_MESSAGE);
            return result;
        } else {
            result.setCode(ResultCode.EXCEPTION_ERROR);
            result.setMessage(failMsg);
            return result;
        }
    }

    public static Result<?> genResult(int rows, String failMsg) {
        Result<?> result = new Result<>();
        if (rows > 0) {
            result.setCode(ResultCode.SUCCESS);
            result.setMessage(DEFAULT_SUCCESS_MESSAGE);
            return result;
        } else {
            result.setCode(ResultCode.EXCEPTION_ERROR);
            result.setMessage(failMsg);
            return result;
        }
    }

}
